home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / winword / cpretty2.zip / prettyc2.txt
Text File  |  1994-04-07  |  12KB  |  306 lines

  1. 'Free WordBasic Macro to make C programming code look nicer when printed.
  2. '(for Word for Windows 6)
  3.  
  4. ' Instructions: open this document in MS-Word for Windows, select the whole document
  5. ' and copy it to the clipboard. Open a new macro (i.e. the 'Tools' menu, 'Macro...'), call it
  6. ' whatever you like. Delete the 'Sub Main' and 'End Sub' lines, so you have a blank page,
  7. ' then Paste this document into that macro, and voila! there you have it.
  8. '
  9. ' Pretty C Mk II
  10. '
  11. ' Howard Jones, 1994 (ha.jones@ic.ac.uk)
  12. ' Modified from PrettyC by 
  13. ' Phillip Ryan, DSTO Materials Research Laboratory, Melbourne, Australia 
  14. ' (ryanph@mrl.dsto.gov.au)
  15. ' Now supports C++ comments, Word 6.0 (no longer Word 2.x - Sorry!)
  16. ' and extra C++ reserved words.
  17. ' Also altered order so there are no keyword-in-comment problems
  18. ' and added subroutine structure so it is a *little* neater :)
  19. ' [It should be fairly trivial to port back to Word 2.x, BTW         ]
  20. ' [EditFind to EditSearch, and EditFindFont to EditSearchChar, mostly]
  21. '
  22. ' This is a free macro - if your fix any bugs, add any more goodies
  23. ' or otherwise improve this macro, please send me a copy (email if poss)
  24. ' (also, one for Phillip Ryan, too - it was his baby!)
  25. '
  26. ' Howard Jones, 12 Fountains Garth, Bracknell, Berkshire, RG12 7RH, UK.
  27. '
  28. ' It does no actual code formatting - for that, use GNU Indent
  29. '
  30. ' ********************************************************
  31. ' CAUTION!
  32. ' This will clear any formatting in the document before it starts!
  33. ' Don't do any special prettifying until *after* running this!
  34. ' ********************************************************
  35.  
  36. Sub MAIN
  37.     StartOfDocument
  38.  
  39. ' first, flush all the formatting info - for a clean sheet
  40.  
  41.     EditSelectAll
  42.     ResetChar
  43.  
  44. ' do the actual formatting
  45.     Call FormatCComments
  46.     Call FormatCPlusPlusComments
  47.     Call FormatDirectives
  48.     Call FormatANSIKeys
  49.     Call FormatStdioKeys
  50.     Call FormatCPlusPlusKeys
  51.  
  52. 'Notify we're finished, and clear up
  53.     Beep
  54.     Print "Pretty C is finished"
  55. End Sub
  56.  
  57. '*******************************************************************************
  58. ' Subroutine to format the C++ style comments
  59. Sub FormatCPlusPlusKeys
  60.  
  61. Print "Formatting C++ Keywords"
  62.  
  63. ' For the first set of words, make the format of the replaced text be bold only.
  64. EditReplaceFont .Bold = 1, .Italic = 0, .Underline = 0, .Strikethrough = 0
  65.  
  66. ' Start doing the replacing.
  67. EditReplace .Find = "virtual", .Replace = "virtual", .Format = 1, .WholeWord = 1, .ReplaceAll
  68. StartOfDocument
  69. EditReplace .Find = "class", .Replace = "class", .Format = 1, .WholeWord = 1, .ReplaceAll
  70. StartOfDocument
  71. EditReplace .Find = "private:", .Replace = "private:", .Format = 1, .WholeWord = 1, .ReplaceAll
  72. StartOfDocument
  73. EditReplace .Find = "public:", .Replace = "public:", .Format = 1, .WholeWord = 1, .ReplaceAll
  74. StartOfDocument
  75. StartOfDocument
  76. EditReplace .Find = "public", .Replace = "public", .Format = 1, .WholeWord = 1, .ReplaceAll
  77. StartOfDocument
  78. EditReplace .Find = "inline", .Replace = "inline", .Format = 1, .WholeWord = 1, .ReplaceAll
  79. StartOfDocument
  80. EditReplace .Find = "protected:", .Replace = "protected:", .Format = 1, .WholeWord = 1, .ReplaceAll
  81. StartOfDocument
  82. EditReplace .Find = "friend", .Replace = "friend", .Format = 1, .WholeWord = 1, .ReplaceAll
  83. StartOfDocument
  84.  
  85. End Sub
  86.  
  87. '*******************************************************************************
  88. ' Subroutine to format the C++ style comments
  89. Sub FormatCPlusPlusComments
  90.  
  91. Print "Formatting C++ Comments"
  92.  
  93. StartOfDocument
  94.  
  95. ' Look for the normal comment string delimiter of C.
  96. EditFindClearFormatting
  97. EditFindFont .Italic = 0
  98.  
  99. EditFind .Find = "//", .Format = 1
  100.  
  101. ' Keep on searching for comment blocks.
  102. While EditFindFound()
  103.     ExtendSelection
  104.     EditFind .Find = "^p"
  105.     FormatFont .Italic = 1, .Bold = 0
  106.     Cancel
  107.     
  108. ' To ensure that the next found character is in the _next_ comment block,
  109. ' we set the findchar type to italic _off_. (Since otherwise the insertion
  110. ' point always remains at the start of the last selection.)
  111.     StartOfDocument
  112.     EditFind .Find = "//", .Format = 1
  113. Wend
  114.  
  115. StartOfDocument
  116.  
  117. End Sub
  118.  
  119. '*******************************************************************************
  120. ' Subroutine to format the C style comments
  121. Sub FormatCComments
  122.  
  123. Print "Formatting C Comments"
  124.  
  125. ' Now, to format the comments.
  126. StartOfDocument
  127.  
  128. ' Look for the normal comment string delimiter of C.
  129. EditFindClearFormatting
  130. EditFindFont .Italic = 0
  131.  
  132. EditFind .Find = "/*", .Format = 1
  133.  
  134. ' Keep on searching for comment blocks.
  135. While EditFindFound()
  136.     ExtendSelection
  137.     EditFind .Find = "*/"
  138.     FormatFont .Italic = 1, .Bold = 0
  139.     Cancel
  140.     
  141. ' To ensure that the next found character is in the _next_ comment block,
  142. ' we set the findchar type to italic _off_. (Since otherwise the insertion
  143. ' point always remains at the start of the last selection.)
  144.     StartOfDocument
  145.     EditFind .Find = "/*", .Format = 1
  146. Wend
  147.  
  148. StartOfDocument
  149.  
  150. End Sub
  151.  
  152. '*******************************************************************************
  153. ' Subroutine to format the pre-processor keywords
  154.  
  155. Sub FormatDirectives
  156.  
  157. Print "Formatting Pre-processor Directives"
  158.  
  159. ' For the last set, the C pre-processor directives, make the format bold-italic only.
  160. EditReplaceFont .Bold = 1, .Italic = 1, .Underline = 0, .Strikethrough = 0
  161. EditReplace .Find = "#include", .Replace = "#include", .Format = 1, .WholeWord = 1, .ReplaceAll
  162. StartOfDocument
  163. EditReplace .Find = "#define", .Replace = "#define", .Format = 1, .WholeWord = 1, .ReplaceAll
  164. StartOfDocument
  165. EditReplace .Find = "#ifdef", .Replace = "#ifdef", .Format = 1, .WholeWord = 1, .ReplaceAll
  166. StartOfDocument
  167. EditReplace .Find = "#error", .Replace = "#error", .Format = 1, .WholeWord = 1, .ReplaceAll
  168. StartOfDocument
  169. EditReplace .Find = "#ifndef", .Replace = "#ifndef", .Format = 1, .WholeWord = 1, .ReplaceAll
  170. StartOfDocument
  171. EditReplace .Find = "#if", .Replace = "#if", .Format = 1, .WholeWord = 1, .ReplaceAll
  172. StartOfDocument
  173. EditReplace .Find = "#else", .Replace = "#else", .Format = 1, .WholeWord = 1, .ReplaceAll
  174. StartOfDocument
  175. EditReplace .Find = "#elif", .Replace = "#elif", .Format = 1, .WholeWord = 1, .ReplaceAll
  176. StartOfDocument
  177. EditReplace .Find = "#endif", .Replace = "#endif", .Format = 1, .WholeWord = 1, .ReplaceAll
  178. StartOfDocument
  179. EditReplace .Find = "#pragma", .Replace = "#pragma", .Format = 1, .WholeWord = 1, .ReplaceAll
  180. StartOfDocument
  181.  
  182. End Sub
  183.  
  184.  
  185. '*******************************************************************************
  186. ' Subroutine to format the stdio keywords
  187. Sub FormatStdioKeys
  188.  
  189. Print "Formatting stdio Functions/Types"
  190.  
  191. 'For this next set, which is a small subset of the standard libraries, make the replaced text 
  192. 'format be italic only.
  193.  
  194. EditReplaceFont .Bold = 0, .Italic = 1, .Underline = 0, .Strikethrough = 0
  195. StartOfDocument
  196. EditReplace .Find = "main", .Replace = "main", .Format = 1, .WholeWord = 1, .ReplaceAll
  197. StartOfDocument
  198. EditReplace .Find = "printf", .Replace = "printf", .Format = 1, .WholeWord = 1, .ReplaceAll
  199. StartOfDocument
  200. EditReplace .Find = "fprintf", .Replace = "fprintf", .Format = 1, .WholeWord = 1, .ReplaceAll
  201. StartOfDocument
  202. EditReplace .Find = "scanf", .Replace = "scanf", .Format = 1, .WholeWord = 1, .ReplaceAll
  203. StartOfDocument
  204. EditReplace .Find = "fscanf", .Replace = "fscanf", .Format = 1, .WholeWord = 1, .ReplaceAll
  205. StartOfDocument
  206. EditReplace .Find = "getc", .Replace = "getc", .Format = 1, .WholeWord = 1, .ReplaceAll
  207. StartOfDocument
  208. EditReplace .Find = "getch", .Replace = "getch", .Format = 1, .WholeWord = 1, .ReplaceAll
  209. StartOfDocument
  210. EditReplace .Find = "getchar", .Replace = "getchar", .Format = 1, .WholeWord = 1, .ReplaceAll
  211. StartOfDocument
  212. EditReplace .Find = "putc", .Replace = "putc", .Format = 1, .WholeWord = 1, .ReplaceAll
  213. StartOfDocument
  214. EditReplace .Find = "putch", .Replace = "putch", .Format = 1, .WholeWord = 1, .ReplaceAll
  215. StartOfDocument
  216. EditReplace .Find = "putchar", .Replace = "putchar", .Format = 1, .WholeWord = 1, .ReplaceAll
  217. StartOfDocument
  218. EditReplace .Find = "FILE", .Replace = "FILE", .Format = 1, .WholeWord = 1, .MatchCase = 1, .ReplaceAll
  219. StartOfDocument
  220.  
  221. End Sub
  222.  
  223. '*******************************************************************************
  224. ' Subroutine to format all the ANSI Keywords in the document
  225. Sub FormatANSIKeys
  226.  
  227. Print "Formatting ANSI C Keywords"
  228.  
  229. ' For the first set of words, make the format of the replaced text be bold only.
  230. EditReplaceFont .Bold = 1, .Italic = 0, .Underline = 0, .Strikethrough = 0
  231.  
  232. ' Start doing the replacing.
  233. EditReplace .Find = "float", .Replace = "float", .Format = 1, .WholeWord = 1, .ReplaceAll
  234. StartOfDocument
  235. EditReplace .Find = "long", .Replace = "long", .Format = 1, .WholeWord = 1, .ReplaceAll
  236. StartOfDocument
  237. EditReplace .Find = "int", .Replace = "int", .Format = 1, .WholeWord = 1, .ReplaceAll
  238. StartOfDocument
  239. EditReplace .Find = "double", .Replace = "double", .Format = 1, .WholeWord = 1, .ReplaceAll
  240. StartOfDocument
  241. EditReplace .Find = "if", .Replace = "if", .Format = 1, .WholeWord = 1, .ReplaceAll
  242. StartOfDocument
  243. EditReplace .Find = "then", .Replace = "then", .Format = 1, .WholeWord = 1, .ReplaceAll
  244. StartOfDocument
  245. EditReplace .Find = "else", .Replace = "else", .Format = 1, .WholeWord = 1, .ReplaceAll
  246. StartOfDocument
  247. EditReplace .Find = "while", .Replace = "while", .Format = 1, .WholeWord = 1, .ReplaceAll
  248. StartOfDocument
  249. EditReplace .Find = "until", .Replace = "until", .Format = 1, .WholeWord = 1, .ReplaceAll
  250. StartOfDocument
  251. EditReplace .Find = "repeat", .Replace = "repeat", .Format = 1, .WholeWord = 1, .ReplaceAll
  252. StartOfDocument
  253. EditReplace .Find = "return", .Replace = "return", .Format = 1, .WholeWord = 1, .ReplaceAll
  254. StartOfDocument
  255. EditReplace .Find = "break", .Replace = "break", .Format = 1, .WholeWord = 1, .ReplaceAll
  256. StartOfDocument
  257. EditReplace .Find = "case", .Replace = "case", .Format = 1, .WholeWord = 1, .ReplaceAll
  258. StartOfDocument
  259. EditReplace .Find = "switch", .Replace = "switch", .Format = 1, .WholeWord = 1, .ReplaceAll
  260. StartOfDocument
  261. EditReplace .Find = "repeat", .Replace = "repeat", .Format = 1, .WholeWord = 1, .ReplaceAll
  262. StartOfDocument
  263. EditReplace .Find = "register", .Replace = "register", .Format = 1, .WholeWord = 1, .ReplaceAll
  264. StartOfDocument
  265. EditReplace .Find = "volatile", .Replace = "volatile", .Format = 1, .WholeWord = 1, .ReplaceAll
  266. StartOfDocument
  267. EditReplace .Find = "union", .Replace = "union", .Format = 1, .WholeWord = 1, .ReplaceAll
  268. StartOfDocument
  269. EditReplace .Find = "typedef", .Replace = "typedef", .Format = 1, .WholeWord = 1, .ReplaceAll
  270. StartOfDocument
  271. EditReplace .Find = "struct", .Replace = "struct", .Format = 1, .WholeWord = 1, .ReplaceAll
  272. StartOfDocument
  273. EditReplace .Find = "auto", .Replace = "auto", .Format = 1, .WholeWord = 1, .ReplaceAll
  274. StartOfDocument
  275. EditReplace .Find = "const", .Replace = "const", .Format = 1, .WholeWord = 1, .ReplaceAll
  276. StartOfDocument
  277. EditReplace .Find = "continue", .Replace = "continue", .Format = 1, .WholeWord = 1, .ReplaceAll
  278. StartOfDocument
  279. EditReplace .Find = "default", .Replace = "default", .Format = 1, .WholeWord = 1, .ReplaceAll
  280. StartOfDocument
  281. EditReplace .Find = "enum", .Replace = "enum", .Format = 1, .WholeWord = 1, .ReplaceAll
  282. StartOfDocument
  283. EditReplace .Find = "extern", .Replace = "extern", .Format = 1, .WholeWord = 1, .ReplaceAll
  284. StartOfDocument
  285. EditReplace .Find = "goto", .Replace = "goto", .Format = 1, .WholeWord = 1, .ReplaceAll
  286. StartOfDocument
  287. EditReplace .Find = "short", .Replace = "short", .Format = 1, .WholeWord = 1, .ReplaceAll
  288. StartOfDocument
  289. EditReplace .Find = "signed", .Replace = "signed", .Format = 1, .WholeWord = 1, .ReplaceAll
  290. StartOfDocument
  291. EditReplace .Find = "unsigned", .Replace = "unsigned", .Format = 1, .WholeWord = 1, .ReplaceAll
  292. StartOfDocument
  293. EditReplace .Find = "sizeof", .Replace = "sizeof", .Format = 1, .WholeWord = 1, .ReplaceAll
  294. StartOfDocument
  295. EditReplace .Find = "static", .Replace = "static", .Format = 1, .WholeWord = 1, .ReplaceAll
  296. StartOfDocument
  297. EditReplace .Find = "void", .Replace = "void", .Format = 1, .WholeWord = 1, .ReplaceAll
  298. StartOfDocument
  299. EditReplace .Find = "char", .Replace = "char", .Format = 1, .WholeWord = 1, .ReplaceAll
  300.  
  301. StartOfDocument
  302. End Sub
  303.  
  304.  
  305.  
  306.